package com.gongzhongzhisheng.robot.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gongzhongzhisheng.robot.mapper.KeywordMapper;
import com.gongzhongzhisheng.robot.mapper.RobotMapper;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping
@Api(value = "机器人回复",tags = {"机器人回复"})
public class RobotController {
	
	
	@Autowired
	private RobotMapper robotMapper;
	
	@PostMapping
	public void dopost(HttpServletRequest req, HttpServletResponse resp)throws Exception {
		
		req.setCharacterEncoding("UTF-8");  
        resp.setCharacterEncoding("UTF-8");  
        PrintWriter out =resp.getWriter();  
          try {  
            Map<String, String> map=MessageUtil.xmlToMap(req);  
            String toUserName=map.get("ToUserName");  
            String fromUserName=map.get("FromUserName");  
            String msgType=map.get("MsgType");  
            String content=map.get("Content");
            
            String message=null;          
            if("text".equals(msgType)){  
            //  System.out.println("text.equals(msgType)");  
                TextMessage text =new TextMessage();  
                text.setFromUserName(toUserName);  
                text.setToUserName(fromUserName);  
                text.setMsgType("text");  
                //这里填写回复内容  
                StringBuffer sb = new StringBuffer();
                sb.append(" ");
                KeywordMapper km = robotMapper.selectKeyword();
        		String[] keyword = km.getKeyword().split(",");
        		
        		for(int i=0;i<keyword.length;i++) {
        			
        			if(content.contains(keyword[i])) {
        				List<KeywordMapper> kmList = robotMapper.selectKeywordList(keyword[i]);
        				for(int j=0;j<kmList.size();j++) {
        					KeywordMapper kms = kmList.get(j);
        					if(!sb.toString().contains(kms.getReplyCapacity())) {
        						sb.append(kms.getReplyCapacity());
        						sb.append("\n");
        					}
        				}
        			}
        		}
        		
                text.setContent(sb.toString());
                
                text.setCreateTime(new Date().getTime());  
                message=MessageUtil.textMessageToXml(text);  
            }
          out.print(message);  
        } catch (Exception e) {  
            //System.out.println("doPost->try..catch");  
            e.printStackTrace();  
        }finally{  
            out.close();  
        }  
	}
	
	
	@GetMapping
	public void doget(HttpServletRequest req, HttpServletResponse resp)throws Exception {
		
		String signature=req.getParameter("signature");
        String timestamp=req.getParameter("timestamp");
        String nonce=req.getParameter("nonce");
        String echostr=req.getParameter("echostr");
        PrintWriter out =resp.getWriter();  
        if(WeChatConnectValidateUtil.checkSignature(signature,timestamp,nonce)){  
            out.print(echostr);  
        }  
	}
}
